Oitsm.Frame = Ext.extend(Ext.Panel, {
	layout : 'border',
	containerPanel : null,
	border : false,
	
	initComponent : function(){
		Oitsm.Frame.superclass.initComponent.call(this);
		this.buildMenu();
		this.buildMainPanel();
	},
	
	buildMenu : function(){
		var menuPanel = new Ext.Panel({
			margins : {
				left : 5,
			    top: 5,
			    bottom: 5
			},
			region : 'west',
			width : 200,
			split : true,
			layout : 'accordion',
			collapsible : true,
			collapseMode : 'header',
			items : []
		});
		
		this.add(menuPanel);
		
		Ext.Ajax.request({
			url : webContext + '/menu_getMenus.action',
			success : function(response, opt) {
				var ja = Ext.decode(response.responseText);
				for(i=0;i<ja.length;i++){
					var jo = ja[i];
					
					var groupPanel = new Ext.tree.TreePanel( {
					    title: jo.text,
					    width: 200,
					    height: 150,
					    border : false,
					    root : {
					    	text : '',
							expanded : true,
							children : jo.children
					    },
					    rootVisible: false
					});
					menuPanel.add(groupPanel);
					groupPanel.addListener("itemclick",this.menuClick,this);
				}
				menuPanel.doLayout();
			},
			failure : function(response, opt) {
				Ext.Msg.alert("提示ʾ","菜单加载失败!");
				this.initCompleted = true;
				Ext.getCmp('itilMainPanel').loadCompletedCallback();
			},
			scope : this
		});
	},
	
	menuClick : function(tree, r, item, index, e){
		if(!r.raw.className){
			return ;
		}
		
		var loadMask;
		var tabPanel = this.containerPanel;
		var tab = tabPanel.child('#_tab'+r.raw.id);
		if(tab && tab.isSuccess){
			tabPanel.setActiveTab(tab);	
			return ;
		}
		
		if(tab){
			tab.removeAll();
			tab.doLayout();
		}else{
			tab = new Ext.Panel({
				border : false,
				layout : 'fit',
				closable : true,
				isSuccess : false,
				items : []
			});
			tab.id = '_tab'+r.raw.id;
			tab.setTitle(r.raw.text);
			tabPanel.add(tab);
		}
		
		tabPanel.setActiveTab(tab);
		
		if(r.raw.className!='' && r.raw.jsPaths!=''){
			loadMask = new Ext.LoadMask(tab.getEl()); 
			loadMask.show();   
			Oitsm.loadUtil.load(r.raw.jsPaths,function(isSuccess,logs){
				if(isSuccess){
					var contentPanel = Ext.create(r.raw.className,r.raw.param);
					contentPanel.border = false;
					tab.add(contentPanel);
					tab.isSuccess = true;
				}
				if(logs.length!=0){
					var errorLog = '';
					for(l=0;l<logs.length;l++){
						errorLog += logs[l] + '<br>';
					}
					tab.add({
						border : false,
						html : '加载失败,原因:<br>' + errorLog
					});
				}
				loadMask.hide();
				tab.doLayout();
			},this);
		}
	},
	
	
	buildMainPanel : function(){
		var home = new Oitsm.HomePanel();
		
		var panel = new Ext.TabPanel({
			id : 'mainPanel',
			region : 'center',
			margins : {
			    top: 5,
			    right : 5,
			    bottom: 5
			},
			activeTab : 0,
			items : [home]
		});
		
		this.containerPanel = panel;
		
		this.add(panel);
	}
});
